<template>
    <main-wrap ref="mianEle" :loading="true" :load="onLoad" bodyBg="none" :noFixed="true" :end="page.finished" :showOverTxt="page.lists.length>0" :pageIndex="page.pageIndex">
        <!-- <template v-slot:header>
            <div class="adheader">
                <div class="hcon">
                    <div class="sell-cp-zhtxt">
                        <two-tab v-model="typeId" :lists="[{id:1,name:'Initiated Auction'},{id:2,name:'Involved Auction'},{id:3,name:'Initiated Direct Sale'},{id:4,name:'Purchase Direct Sale'}]" @change="resetScroll"/>
                    </div>
                    <div class="topSearch">
                        <div class="tswd">
                            <my-search placeholder="Search" v-model="keyword" />
                        </div>
                    </div>
                </div>
            </div>
        </template> -->
        <div class="main-con">
            <!-- <div class="ctgbox">
                <two-tab v-model="ggggg" :lists="kkkkk" @change="getSearchData" keyValue="goodsCategoryId"/>
            </div>
            <div></div>
            <div class="sell-goodsTabSearchBox">
                <sort :lists="[{id:1,name:'Default',noShow:true},{id:2,name:'Value'},{id:3,name:'Comprehensive Flow'},{id:4,name:'Income'}]" @change="getSortData"/>
            </div> -->
            <goods-list :lists="page.lists" @onClick="openDetail" :type="7"/>
            <empty v-if="page.finished && page.lists.length<1"/>
        </div>
    </main-wrap>
    <my-dailog v-model:show="showFlag" :showtopclose="true" :styleType="5">
            <detail :id="itemDetail.orderId" :detail="itemDetail"/>
        </my-dailog>
</template>

<script>
import {reactive,toRefs, onMounted,computed,getCurrentInstance,onBeforeUnmount } from 'vue'
import { useStore } from 'vuex'
import {useRoute,useRouter} from 'vue-router'
import mainWrap from '@/components/mainWrap.vue'
import {toast,myOverlay} from '@/utils/common.js'
import oneTab from '@/components/oneTab.vue';
import mySearch from '@/components/mySearch.vue'
import Image from '@/components/image.vue';
import myTab from '@/components/myTab.vue';
import goodsList from '@/components/goodsList.vue';
import detail from './components/detail.vue'
import myDailog from '@/components/myDailog.vue'
import twoTab from '@/components/twoTab.vue'
import empty from '@/components/empty.vue'
export default {
    components:{
        mainWrap,
        oneTab,
        mySearch,
        Image,
        myTab,
        goodsList,
        detail,
        myDailog,
        twoTab,
        empty
    },
    setup(){
        const store = useStore()
        const route = useRoute()
        const {proxy} = getCurrentInstance()

        const state = reactive({
            userInfo:computed(()=>store.state.base.userInfo),
            keyword:'',
            page:{
                lists:[],
                pageIndex:1,
                pageSize:20,
                finished: false
            },
            sortParams:{
                orderField: 1,
                orderType: "desc",
            },
            showFlag:false,
            typeId:1,
            countryLists:[],
            countryId:'',
            sellType:'',
            itemDetail:{},
            st1:'',
            st2:''
        })

        onMounted(()=>{
            myOverlay.open()
            //onLoad()
            if(route.query.id){
                // 直接打开对应id的详情弹窗
                state.itemDetail.orderId = route.query.id
                state.showFlag = true
            }
            state.st1 = setTimeout(()=>{onLoad()},2000)
        })

        const myClearTimeOut = ()=>{
            clearTimeout(state.st1)
            clearTimeout(state.st2)
        }

        onBeforeUnmount(()=>{  // 销毁组件
            myClearTimeOut()
        })

        const onLoad = ()=> {
            // 此函数必须返回一个Promise对象
            return new Promise((resolve, reject) => {
                state.st2 = setTimeout(async () => {
                    const res = await store.dispatch("collection/get_number_goods_list", {
                        page: state.page.pageIndex,
                        limit: state.page.pageSize,
                        orderField: '',
                        orderType: '',
                        userAddress:state.userInfo.walletAddress
                    });
                    state.page.lists = [...state.page.lists, ...res.data];
                    state.page.pageIndex++;
                    if (state.page.lists.length >= res.total) {
                        state.page.finished = true;
                    }
                    myOverlay.close()
                    resolve(true);
                }, 500);
            });
        }

        const openDetail = item=>{
            state.itemDetail = item
            state.showFlag = true
        }      

        const resetScroll = async()=>{
            myOverlay.open()
            proxy.$refs.mianEle.stopScroll()
            state.page.lists=[]
            state.page.pageIndex=1
            state.page.finished=false
            await onLoad()
            proxy.$refs.mianEle.initScroll()
        }
        const getSearchData = async(keyword)=>{
            resetScroll()
        }

        const getSortData = sortParams =>{
            state.sortParams = sortParams
            resetScroll()
        }

        return {
            ...toRefs(state),
            onLoad,
            openDetail,
            resetScroll,
            getSearchData,
            getSortData
        }
    }
}
</script>

<style lang="scss" scoped>
    .adheader{
         padding: 15px 30px 15px 0;
        .hcon{
            display: flex;
            height: 80px;
            line-height: 80px;
            align-items: center;
            em{
                display: block;
                font-size: 34px;
                font-style: normal;
                text-shadow: -1px 0px 0px rgba(0,209,255,0.7000), 1px 0px 0px rgba(255,0,184,0.7000);
                //-webkit-background-clip: text;
                //-webkit-text-fill-color: transparent;
            }
            .sort{
                padding: 0 0 0 30px;
            }
            .topSearch{
                flex: 1;
                // display: flex;
                // justify-content: end;
                .tswd{
                    float:right;
                    width: 550px;
                }
            }
            .sell-cp-zhtxt{
                padding: 0 30px 0 0;
            }
        }
    }
    .main-con{
        padding: 10px 0 0 0;
    }
    .sell-tabSearchBox{
        display: inline-block;
        height: 65px;
        background: url(/images/gloabAdSplace/tsc.png) left top no-repeat;
        background-size: 100% 100%;
        padding: 0 30px 0 0;
        .item{
            display: inline-block;
            padding:0 0 0 30px;
            line-height: 65px;
            font-weight: 500;
            color: rgba(255,255,255,0.5000);
            font-size: 28px;
            &.selected{
                color: #fff;
                font-weight: bold;
            }
        }
    }
    .ctgbox{
        display: inline-block;
        width: auto;
        padding: 0 0 20px 0;
    }
    .sell-goodsTabSearchBox{
        display: inline-block;
        height: 65px;
        background: url(/images/gloabAdSplace/tsc.png) left top no-repeat;
        background-size: 100% 100%;
        padding: 0 30px;
    }
</style>